<%@ page language="java" import="com.alibaba.fastjson.JSONObject" pageEncoding="UTF-8" %>
<%@ page import="okhttp3.*" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.interfaces.zhlx.duss.utils.PmsUtil" %>
<%@ page import="java.util.concurrent.TimeUnit" %>
<%--OA流程节点信息PMS系统接口回传--%>
<%
    JSONObject jsonData = new JSONObject();
    BaseBean baseBean = new BaseBean();
    RecordSet rs = new RecordSet();
    String message = "";
    String status = "success";
    String requestid = Util.null2String(request.getParameter("requestid"));
    int formid = Util.getIntValue(request.getParameter("formid"));
    formid = Math.abs(formid);
    String src = Util.null2String(request.getParameter("src"));
    try {
        rs.executeQuery("select htly,pmsgdh,lcbh,jkdz from formtable_main_" + formid + " where requestid=?", requestid);
        if (rs.next()) {
            String htly = Util.null2String(rs.getString("htly"));
            String pmsgdh = Util.null2String(rs.getString("pmsgdh"));
            String lcbh = Util.null2String(rs.getString("lcbh"));
            String jkdz = Util.null2String(rs.getString("jkdz"));
            String url = jkdz + "/ECSAP2PMS";
            if (!"0".equals(htly)) { 
                JSONObject paramJson = new JSONObject();
                String zt = "1";
                int nextnodeid = 0;
                rs.executeQuery("select distinct userid,nodeid from workflow_currentoperator\n" +
                        " where (isremark in ('0','1','5','7','8','9') or (isremark='4' and viewtype=0))  and requestid = ?", requestid);
                while (rs.next()) {
                    nextnodeid = Util.getIntValue(rs.getString("nodeid"));
                }
                JSONObject reusltJson = PmsUtil.getNodebaseNodename(nextnodeid);
                String nodename = Util.null2String(reusltJson.getString("nodename"));
                String isend = Util.null2String(reusltJson.getString("isend"));
                if ("1".equals(isend)) {
                    if ("reject".equals(src) && "退回归档勿修改".equals(nodename)) {
                        zt = "0";
                    }
                }
                paramJson.put("pmsno", pmsgdh);
                paramJson.put("requestid", requestid + "_" + lcbh);
                paramJson.put("status", zt);
                paramJson.put("batchno", "");
                paramJson.put("type", "EC");
                baseBean.writeLog(">>>>>WeaCbzxlylcBackAction>>>>>paramJson=" + paramJson);
                JSONObject resultJson = handleOKHttpRequest(paramJson, url);
                baseBean.writeLog(">>>>>WeaCbzxlylcBackAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    JSONObject dataJson = JSONObject.parseObject(resultJson.getString("data"));
                    JSONObject outputJson = JSONObject.parseObject(dataJson.getString("OUTPUT"));
                    String msgType = outputJson.getString("MSG_TYPE");
                    String msgText = outputJson.getString("MSG_TEXT");
                    if (!"S".equalsIgnoreCase(msgType)) {
                        status = "failture";
                        message = msgText;
                    }
                } else {
                    message = Util.null2String(resultJson.getString("message"));
                    status = "failture";
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    jsonData.put("status", status);
    jsonData.put("message", message);
    out.print(jsonData.toJSONString());
%>

<%!

    /**
     * 请求处理器
     *
     * @param paramsJson
     * @param url
     * @return
     */
    public static JSONObject handleOKHttpRequest(JSONObject paramsJson, String url) {
        return sendOKHttpRequest(paramsJson, url);
    }

    private static JSONObject sendOKHttpRequest(JSONObject params, String url) {
        JSONObject resultJson = new JSONObject();
        String code = "failure";
        String message = "";
        String data = "";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, params.toJSONString());
            Request request = new Request.Builder()
                    .url(url)
                    .method("POST", body)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();
            String result = Util.null2String(response.body().string());
            if (!"".equals(result)) {
                data = result;
                code = "success";
            } else {
                message = "response result is null";
            }
        } catch (Exception e) {
            message = "response timeout";
            e.printStackTrace();
        }
        resultJson.put("code", code);
        resultJson.put("data", data);
        resultJson.put("message", message);
        return resultJson;
    }

%>


